/*

Input: tmp/wl_sb_assignments_clean.dta [Assignment of precinct to PP in each election]

Output: newdata/wahllokal_change.dta [panel of PP, with activity status and category]

Main tasks:
A) Build balanced panel of polling places (PP) (German: Wahllokale (wl)) with dummy if PP is active in election t (wl_active)
B) Gen variables of interest at the PP level, esp. for changes in PP activity status 
	over time & type of PP (school, restaurant, retirement  home, etc.)
	
Note: Vars at PP-level characteristics have prefix wl_	


*/


 ** PULL: Assignment of precint to PP in each election  
 use "$tmp/wl_sb_assignments_clean.dta", clear 
	isid sb wahl_id
	
	// KEEP unique PP in each election 
	duplicates drop wl_id wahl_id, force 
	drop sb wahl
	
	// FILL to create BALANCED panel (total of 9 elections, incl. FE-09)
	fillin wl_id wahl_id
	sort wl_id wahl_id
	
	// fill in other vars  (missings created)
	foreach v of varlist wl_typ lon_wl lat_wl {
		bys wl_id (_fillin): replace `v'=`v'[1] if missing(`v')
	}
	
	// gen wl_active: dummy that tracks PP's activity status over 9 elections
	recode _fillin (0=1) (1=0), gen(wl_active)
	lab var wl_active "Dummy (PP active in election t)"
	drop _fillin
	
	
	// wl_dropped: dummy = 1 if WL was active in t-1 and inactive in t
	bys wl_id (wahl_id): gen tmp1 = wl_active==wl_active[_n-1]
	gen wl_dropped = (tmp1==0 & wl_active==0)
	replace wl_dropped=0 if wahl_id==0
	lab var wl_dropped "Dummy (PP was active in t-1 and inactive now)"
	
	// wl_dropped_lag: dummy = 1 if WL is active in t and will be inactive in t+1
	bys wl_id (wahl_id): gen wl_dropped_lag = wl_dropped[_n+1]
	replace wl_dropped_lag=0 if wl_dropped_lag==.
	lab var wl_dropped_lag "Dummy (PP active now but inactive in t+1)"
	
	// wl_added: dummy = 1 if WL inactive in t-1 and active in t
	gen wl_added = (tmp1==0 & wl_active==1)
	replace wl_added=0 if wahl_id==0
	label var wl_added "Dummy (PP inactive now but active in t-1)"
	drop tmp*	
	
	
  // gen wl_kat := PP categories (eg. school) based on PP description (wl_typ)
	gen 	wl_kat = "x"
	lab var wl_kat "Polling place building type"
	
	gen wl_typ_clean = wl_typ

	replace wl_typ_clean = lower(wl_typ_clean)

	gen tmp = 	strpos(wl_typ_clean, "schul")>0 | strpos(wl_typ_clean, "gymnasium") >0 | ///
				strpos(wl_typ_clean, "bildungs") >0 | strpos(wl_typ_clean, "institut") >0 | ///
				strpos(wl_typ_clean, "sonderpäd") >0 | strpos(wl_typ_clean, "fos")>0  

	replace wl_kat = "Schools and other educational entities" if tmp > 0
	drop tmp*

	gen tmp = 	strpos(wl_typ_clean, "kirche")>0 | strpos(wl_typ_clean, "pfarr")>0 | ///
				strpos(wl_typ_clean, "stift")>0 | strpos(wl_typ_clean, "pfarr")>0
	replace wl_kat = "Church-affiliated facilities" if tmp > 0
	drop tmp*

	gen tmp = 	strpos(wl_typ_clean, "alten")>0 | strpos(wl_typ_clean, "demenz")>0 | ///
				strpos(wl_typ_clean, "senioren")>0 | strpos(wl_typ_clean, "kursana")>0 | ///
				strpos(wl_typ_clean, "kolping")>0 | strpos(wl_typ_clean, "gehörlosen")>0 | ///
				strpos(wl_typ_clean, "pflege")>0
	replace wl_kat = "Retirement and nursing homes" if tmp > 0
	drop tmp*

	gen tmp = 	strpos(wl_typ_clean, "hotel")>0 | strpos(wl_typ_clean, "gasthaus")>0 | ///
				strpos(wl_typ_clean, "gaststätte")>0 | strpos(wl_typ_clean, "restaurant")>0
	replace wl_kat = "Hotels and restaurants" if tmp > 0
	drop tmp*

	gen tmp = 	strpos(wl_typ_clean, "jugend")>0 | strpos(wl_typ_clean, "sport")>0 | ///
				strpos(wl_typ_clean, "turner")>0 | strpos(wl_typ_clean, "freizeit")>0 | ///
				strpos(wl_typ_clean, "esv")>0 | strpos(wl_typ_clean, "mosaik")>0 | ///
				strpos(wl_typ_clean, "psv")>0 
	replace wl_kat = "Youth and sports centers" if tmp > 0
	drop tmp*

	gen tmp = 	strpos(wl_typ_clean, "bibliothek")>0 
	replace wl_kat = "Libraries" if tmp > 0
	drop tmp*
	
	replace wl_kat = "Other" if wl_kat == "x"

	//manual correction
	replace wl_kat = "Other" if wl_typ_clean=="bayer. staatsministerium für gesundheit und pflege"
	replace wl_kat = "Schools and other educational entities" if wl_typ_clean=="städt. berufsschule f.d. hotel-, gaststätten- u. braugewerbe, eingang sieboldstr."
	replace wl_kat = "Schools and other educational entities" if wl_typ_clean=="schulzentrum pfarrer-grimm-str. (neubau)"

	drop wl_typ_clean
	
	* dummy for schools
	gen 	wl_school = wl_kat=="Schools and other educational entities"
	lab var wl_school "Dummy (PP is a school)"



* save: Balanced Panel of Polling locations tracking activity status
	isid wl_id wahl_id
	compress
	save "$newdata/wahllokal_change.dta", replace


